home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / WindowMaker / WPrefs.app / Paths.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-08-02  |  9.6 KB  |  355 lines

  1. /* Paths.c- pixmap/icon paths
  2.  * 
  3.  *  WPrefs - Window Maker Preferences Program
  4.  * 
  5.  *  Copyright (c) 1998 Alfredo K. Kojima
  6.  * 
  7.  *  This program is free software; you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation; either version 2 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  This program is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License
  18.  *  along with this program; if not, write to the Free Software
  19.  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
  20.  *  USA.
  21.  */
  22.  
  23.  
  24. #include "WPrefs.h"
  25. #include <unistd.h>
  26.  
  27. typedef struct _Panel {
  28.     WMFrame *frame;
  29.     char *sectionName;
  30.  
  31.     char *description;
  32.     
  33.     CallbackRec callbacks;
  34.  
  35.     WMWindow *win;
  36.     
  37.     WMTabView *tabv;
  38.  
  39.     WMFrame *pixF;
  40.     WMList *pixL;
  41.     WMButton *pixaB;
  42.     WMButton *pixrB;
  43.  
  44.     WMFrame *icoF;
  45.     WMList *icoL;
  46.     WMButton *icoaB;
  47.     WMButton *icorB;
  48.  
  49.     WMColor *red;
  50.     WMColor *black;
  51.     WMColor *white;
  52.     WMFont *font;
  53. } _Panel;
  54.  
  55.  
  56.  
  57. #define ICON_FILE    "paths"
  58.  
  59.  
  60. static void
  61. addPathToList(WMList *list, int index, char *path)
  62. {
  63.     char *fpath = wexpandpath(path);
  64.     WMListItem *item;
  65.  
  66.     item = WMInsertListItem(list, index, path);
  67.  
  68.     if (access(fpath, X_OK)!=0) {
  69.     item->uflags = 1;
  70.     }
  71.     free(fpath);
  72. }
  73.  
  74.  
  75. static void
  76. showData(_Panel *panel)
  77. {
  78.     proplist_t array, val;
  79.     int i;
  80.     
  81.     array = GetObjectForKey("IconPath");
  82.     if (!array || !PLIsArray(array)) {
  83.     if (array)
  84.         wwarning(_("bad value in option IconPath. Using default path list"));
  85.     addPathToList(panel->icoL, -1, "~/pixmaps");
  86.     addPathToList(panel->icoL, -1, "~/GNUstep/Library/Icons");
  87.     addPathToList(panel->icoL, -1, "/usr/include/X11/pixmaps");
  88.     addPathToList(panel->icoL, -1, "/usr/local/share/WindowMaker/Icons");
  89.     addPathToList(panel->icoL, -1, "/usr/local/share/WindowMaker/Pixmaps");
  90.     addPathToList(panel->icoL, -1, "/usr/share/WindowMaker/Icons");
  91.     } else {
  92.     for (i=0; i<PLGetNumberOfElements(array); i++) {
  93.         val = PLGetArrayElement(array, i);
  94.         addPathToList(panel->icoL, -1, PLGetString(val));
  95.     }
  96.     }
  97.  
  98.     array = GetObjectForKey("PixmapPath");
  99.     if (!array || !PLIsArray(array)) {
  100.     if (array)
  101.         wwarning(_("bad value in option PixmapPath. Using default path list"));
  102.     addPathToList(panel->pixL, -1, "~/pixmaps");
  103.     addPathToList(panel->pixL, -1, "~/GNUstep/Library/WindowMaker/Pixmaps");
  104.     addPathToList(panel->pixL, -1, "/usr/local/share/WindowMaker/Pixmaps");
  105.     } else {
  106.     for (i=0; i<PLGetNumberOfElements(array); i++) {
  107.         val = PLGetArrayElement(array, i);
  108.         addPathToList(panel->pixL, -1, PLGetString(val));
  109.     }
  110.     }
  111. }
  112.  
  113.  
  114. static void
  115. pushButton(WMWidget *w, void *data)
  116. {
  117.     _Panel *panel = (_Panel*)data;
  118.     int i;
  119.  
  120.     /* icon paths */
  121.     if (w == panel->icorB) {
  122.     i = WMGetListSelectedItemRow(panel->icoL);
  123.  
  124.     if (i>=0)
  125.         WMRemoveListItem(panel->icoL, i);
  126.     }
  127.  
  128.     /* pixmap paths */
  129.     if (w == panel->pixrB) {
  130.     i = WMGetListSelectedItemRow(panel->pixL);
  131.  
  132.     if (i>=0)
  133.         WMRemoveListItem(panel->pixL, i);
  134.     }
  135. }
  136.  
  137.  
  138. static void
  139. browseForFile(WMWidget *w, void *data)
  140. {
  141.     _Panel *panel = (_Panel*)data;
  142.     WMFilePanel *filePanel;
  143.  
  144.     filePanel = WMGetOpenPanel(WMWidgetScreen(w));
  145.  
  146.     WMSetFilePanelCanChooseFiles(filePanel, False);
  147.  
  148.     if (WMRunModalFilePanelForDirectory(filePanel, panel->win, "/",
  149.                                         _("Select directory"), NULL) == True) {
  150.         char *str = WMGetFilePanelFileName(filePanel);
  151.  
  152.         if (str) {
  153.             int len = strlen(str);
  154.  
  155.             /* Remove the trailing '/' except if the path is exactly / */
  156.             if (len > 1 && str[len-1] == '/') {
  157.                 str[len-1] = '\0';
  158.                 len--;
  159.             }
  160.             if (len > 0) {
  161.                 WMList *lPtr;
  162.                 int i;
  163.  
  164.                 if (w == panel->icoaB)
  165.                     lPtr = panel->icoL;
  166.                 else if (w == panel->pixaB)
  167.                     lPtr = panel->pixL;
  168.  
  169.                 i = WMGetListSelectedItemRow(lPtr);
  170.                 if (i >= 0) i++;
  171.                 addPathToList(lPtr, i, str);
  172.                 WMSetListBottomPosition(lPtr, WMGetListNumberOfRows(lPtr));
  173.  
  174.                 free(str);
  175.             }
  176.         }
  177.     }
  178. }
  179.  
  180.  
  181.  
  182. static void
  183. paintItem(WMList *lPtr, int index, Drawable d, char *text, int state, 
  184.       WMRect *rect)
  185. {
  186.     int width, height, x, y;
  187.     _Panel *panel = (_Panel*)WMGetHangedData(lPtr);
  188.     WMScreen *scr = WMWidgetScreen(lPtr);
  189.     Display *dpy = WMScreenDisplay(scr);
  190.  
  191.     width = rect->size.width;
  192.     height = rect->size.height;
  193.     x = rect->pos.x;
  194.     y = rect->pos.y;
  195.  
  196.     if (state & WLDSSelected)
  197.     XFillRectangle(dpy, d, WMColorGC(panel->white), x, y, width, 
  198.                height);
  199.     else 
  200.     XClearArea(dpy, d, x, y, width, height, False);
  201.  
  202.     if (state & 1) {
  203.     WMDrawString(scr, d, WMColorGC(panel->red), panel->font, x+4, y, 
  204.              text, strlen(text));
  205.     } else {
  206.     WMDrawString(scr, d, WMColorGC(panel->black), panel->font, x+4, y, 
  207.              text, strlen(text));
  208.     }
  209. }
  210.  
  211.  
  212.  
  213. static void
  214. storeData(_Panel *panel)
  215. {
  216.     proplist_t list;
  217.     proplist_t tmp;
  218.     int i;
  219.     char *p;
  220.     
  221.     list = PLMakeArrayFromElements(NULL, NULL);
  222.     for (i=0; i<WMGetListNumberOfRows(panel->icoL); i++) {
  223.     p = WMGetListItem(panel->icoL, i)->text;
  224.     tmp = PLMakeString(p);
  225.     PLAppendArrayElement(list, tmp);
  226.     }
  227.     SetObjectForKey(list, "IconPath");
  228.     
  229.     list = PLMakeArrayFromElements(NULL, NULL);
  230.     for (i=0; i<WMGetListNumberOfRows(panel->pixL); i++) {
  231.     p = WMGetListItem(panel->pixL, i)->text;
  232.     tmp = PLMakeString(p);
  233.     PLAppendArrayElement(list, tmp);
  234.     }
  235.     SetObjectForKey(list, "PixmapPath");
  236. }
  237.  
  238.  
  239.  
  240. static void
  241. createPanel(Panel *p)
  242. {
  243.     _Panel *panel = (_Panel*)p;
  244.     WMScreen *scr = WMWidgetScreen(panel->win);
  245.     WMTabViewItem *tab;
  246.  
  247.     panel->white = WMWhiteColor(scr);
  248.     panel->black = WMBlackColor(scr);
  249.     panel->red = WMCreateRGBColor(scr, 0xffff, 0, 0, True);
  250.     panel->font = WMSystemFontOfSize(scr, 12);
  251.     
  252.     panel->frame = WMCreateFrame(panel->win);
  253.     WMResizeWidget(panel->frame, FRAME_WIDTH, FRAME_HEIGHT);
  254.     WMMoveWidget(panel->frame, FRAME_LEFT, FRAME_TOP);
  255.     
  256.     panel->tabv = WMCreateTabView(panel->frame);
  257.     WMMoveWidget(panel->tabv, 12, 10);
  258.     WMResizeWidget(panel->tabv, 500, 215);
  259.  
  260.     
  261.  
  262.     /* icon path */
  263.     panel->icoF = WMCreateFrame(panel->frame);
  264.     WMSetFrameRelief(panel->icoF, WRFlat);
  265.     WMResizeWidget(panel->icoF, 230, 210);
  266.  
  267.     tab = WMCreateTabViewItemWithIdentifier(0);
  268.     WMSetTabViewItemView(tab, WMWidgetView(panel->icoF));
  269.     WMAddItemInTabView(panel->tabv, tab);
  270.     WMSetTabViewItemLabel(tab, _("Icon Search Paths"));
  271.     
  272.     panel->icoL = WMCreateList(panel->icoF);
  273.     WMResizeWidget(panel->icoL, 480, 147);
  274.     WMMoveWidget(panel->icoL, 10, 10);
  275.     WMSetListUserDrawProc(panel->icoL, paintItem);
  276.     WMHangData(panel->icoL, panel);
  277.     
  278.     panel->icoaB = WMCreateCommandButton(panel->icoF);
  279.     WMResizeWidget(panel->icoaB, 95, 24);
  280.     WMMoveWidget(panel->icoaB, 293, 165);
  281.     WMSetButtonText(panel->icoaB, _("Add"));
  282.     WMSetButtonAction(panel->icoaB, browseForFile, panel);
  283.     WMSetButtonImagePosition(panel->icoaB, WIPRight);
  284.  
  285.     panel->icorB = WMCreateCommandButton(panel->icoF);
  286.     WMResizeWidget(panel->icorB, 95, 24);
  287.     WMMoveWidget(panel->icorB, 395, 165);
  288.     WMSetButtonText(panel->icorB, _("Remove"));
  289.     WMSetButtonAction(panel->icorB, pushButton, panel);
  290.  
  291.     WMMapSubwidgets(panel->icoF);
  292.  
  293.     /* pixmap path */
  294.     panel->pixF = WMCreateFrame(panel->frame);
  295.     WMSetFrameRelief(panel->pixF, WRFlat);
  296.     WMResizeWidget(panel->pixF, 230, 210);
  297.     
  298.     tab = WMCreateTabViewItemWithIdentifier(0);
  299.     WMSetTabViewItemView(tab, WMWidgetView(panel->pixF));
  300.     WMAddItemInTabView(panel->tabv, tab);
  301.     WMSetTabViewItemLabel(tab, _("Pixmap Search Paths"));
  302.     
  303.     panel->pixL = WMCreateList(panel->pixF);
  304.     WMResizeWidget(panel->pixL, 480, 147);
  305.     WMMoveWidget(panel->pixL, 10, 10);
  306.     WMSetListUserDrawProc(panel->pixL, paintItem);
  307.     WMHangData(panel->pixL, panel);
  308.     
  309.     panel->pixaB = WMCreateCommandButton(panel->pixF);
  310.     WMResizeWidget(panel->pixaB, 95, 24);
  311.     WMMoveWidget(panel->pixaB, 293, 165);
  312.     WMSetButtonText(panel->pixaB, _("Add"));
  313.     WMSetButtonAction(panel->pixaB, browseForFile, panel);
  314.     WMSetButtonImagePosition(panel->pixaB, WIPRight);
  315.     
  316.     panel->pixrB = WMCreateCommandButton(panel->pixF);
  317.     WMResizeWidget(panel->pixrB, 95, 24);
  318.     WMMoveWidget(panel->pixrB, 395, 165);
  319.     WMSetButtonText(panel->pixrB, _("Remove"));
  320.     WMSetButtonAction(panel->pixrB, pushButton, panel);
  321.  
  322.  
  323.     WMMapSubwidgets(panel->pixF);
  324.     
  325.     WMRealizeWidget(panel->frame);
  326.     WMMapSubwidgets(panel->frame);
  327.     
  328.     showData(panel);
  329. }
  330.  
  331.  
  332.  
  333. Panel*
  334. InitPaths(WMScreen *scr, WMWindow *win)
  335. {
  336.     _Panel *panel;
  337.  
  338.     panel = wmalloc(sizeof(_Panel));
  339.     memset(panel, 0, sizeof(_Panel));
  340.  
  341.     panel->sectionName = _("Search Path Configuration");
  342.  
  343.     panel->description = _("Search paths to use when looking for pixmaps\n"
  344.                "and icons.");
  345.  
  346.     panel->win = win;
  347.  
  348.     panel->callbacks.createWidgets = createPanel;
  349.     panel->callbacks.updateDomain = storeData;
  350.     
  351.     AddSection(panel, ICON_FILE);
  352.  
  353.     return panel;
  354. }
  355.